package com.android.accessibility.view;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;

import com.android.accessibility.R;
import com.android.accessibility.activity.HomeActivity;
import com.android.accessibility.utils.Constant;

public class FloatView extends LinearLayout implements View.OnClickListener, OnCheckedChangeListener {

    private GestureDetector mGestureDetector;
    private GestureListener mGestureListener;
    private WindowManager mWindowManager;
    private android.view.WindowManager.LayoutParams mLayoutParams;
    private String mStartClass = HomeActivity.class.getName();

    public FloatView(Context context) {
        super(context);
        init(context);
    }

    public FloatView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mGestureListener = new GestureListener();
        mGestureDetector = new GestureDetector(context, mGestureListener);
        LayoutInflater.from(context).inflate(R.layout.floatview_layout, this);
        View view = null;
        view = findViewById(R.id.button_settings);
        view.setOnClickListener(this);
        CheckBox checkBox = (CheckBox) findViewById(R.id.start_stop);
        checkBox.setOnCheckedChangeListener(this);
    }

    public void setWindowManager(WindowManager manager, android.view.WindowManager.LayoutParams params) {
        mWindowManager = manager;
        mLayoutParams = params;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (mGestureDetector.onTouchEvent(ev)) {
            return true;
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mGestureDetector.onTouchEvent(event)) {
            return true;
        }
        return super.onTouchEvent(event);
    }

    private class GestureListener extends GestureDetector.SimpleOnGestureListener {

        private int mLastX;
        private int mLastY;

        @Override
        public boolean onDown(MotionEvent e) {
            mLastX = (int) e.getRawX();
            mLastY = (int) e.getRawY();
            return super.onDown(e);
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {
            int deltaX = (int) (e2.getRawX() - mLastX);
            int deltaY = (int) (e2.getRawY() - mLastY);
            mLastX = (int) e2.getRawX();
            mLastY = (int) e2.getRawY();
            mLayoutParams.x += deltaX;
            mLayoutParams.y += deltaY;
            mWindowManager.updateViewLayout(FloatView.this, mLayoutParams);
            return true;
        }
    }

    public void setClass(String className) {
        mStartClass = className;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button_settings) {
            if (mStartClass != null) {
                Intent intent = new Intent();
                intent.setClassName(getContext(), mStartClass);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getContext().startActivity(intent);
            }
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        String action = !isChecked ? Constant.ACTION_START : Constant.ACTION_STOP;
        Intent intent = new Intent(action);
        getContext().startService(intent);
    }
}
